/**
 * Copyright (c) 2006-2007 Jinyi.lu
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of GNU LGPL license by gun.org
 * which accompanies this distribution, and is available at
 * http://gnu.feld-it.at/copyleft/lesser.html
 * 
 * Project Homepage : http://code.google.com/p/demou
 * Source Code: svn checkout http://demou.googlecode.com/svn/trunk/ demou
 * Mailing list : jinyi.lu@gmail.com
 *                stonelu@126.com
 *
 *  
 * Contributors:
 *     JinYi.Lu - initial API and implementation
 */
package cn.tworen.demou.io;

import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;


/**
 * Created on  2007-2-22
 *
 * Title       : CmdFactory.java
 * Description : 
 * 
 * @author     : LuJinYi
 * @version    : 1.0
 * @Date       : 2007-2-22
 * History     : 
 * 
 */
public class BeanFactory {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(BeanFactory.class);

	private static final Map<String, String> cmdMap = new HashMap<String, String>();
	static {
		cmdMap.put("flex.messaging.messages.CommandMessage", "cn.tworen.demou.remoting.cmd.Cmd");
		cmdMap.put("flex.messaging.messages.RemotingMessage", "cn.tworen.demou.remoting.cmd.RemoteCmd");
		cmdMap.put("flex.messaging.messages.ErrorMessage", "cn.tworen.demou.remoting.cmd.ErrorCmd");
		cmdMap.put("flex.messaging.messages.SOAPMessage", "cn.tworen.demou.remoting.cmd.SaopCmd");
		cmdMap.put("flex.messaging.messages.HTTPMessage", "cn.tworen.demou.remoting.cmd.HttpCmd");
		cmdMap.put("flex.messaging.messages.AcknowledgeMessage", "cn.tworen.demou.remoting.cmd.BaseCmd");
		cmdMap.put("", "cn.tworen.demou.io.AsBean");
		cmdMap.put("flex.messaging.io.ArrayCollection", "cn.tworen.demou.io.ArrayCollection");
	}
	public static Object getBean(String name) throws Exception {
		String beanName = name;
		if("".equals(name)) {
			logger.debug("init bean as AsBean!");
			return new AsBean();
		} else if (cmdMap.containsKey(name)) {
			beanName = cmdMap.get(name);
		}
		Class className = Class.forName(beanName);
		logger.debug("factory bean.name=" + name + " act.bean=" + className.getName());
		return className.newInstance();
	}
	public static String getBeanClassName(String name) {
		if (cmdMap.containsValue(name)) {
			for(String key: cmdMap.keySet()) {
				if (cmdMap.get(key).equals(name)) {
					return key;
				}
			}
		}
		return name;
	}
	public static PropertyDescriptor[] getPropertyDescriptors(Object o) {
		PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(o.getClass());
		List properties = new ArrayList();
		for(PropertyDescriptor propertyDescriptor:propertyDescriptors) {
			if (propertyDescriptor.getWriteMethod() != null && propertyDescriptor.getReadMethod() != null) {
				properties.add(propertyDescriptor);
			}
		}
		propertyDescriptors = new PropertyDescriptor[properties.size()];
		properties.toArray(propertyDescriptors);
		return propertyDescriptors;
	}
}
